package com.kongchengji.spider.util;
import com.kongchengji.spider.station.domain.PictureDO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
public class ImageByteUtil {
    @Value("${vpn.connectCommand}")
    private  String connectCommand;
//    public  void fetchContent(PictureDO pictureDO, String referValue, Boolean isConnectVPN) throws ClientProtocolException, IOException {
//        InputStream input = null;
//        ByteArrayOutputStream output = null;
//        HttpClient httpclient = HttpClientPool.getHttpClient();
//        HttpGet httpget = new HttpGet(pictureDO.getPictureUrl());
//        //判断是否需要断开vpn
//        if(isConnectVPN){
//            isConnectVPN = false;
//            //三分钟后自动断开
//            new Thread(new ReleaseVPN()).start();
//        }
//        if(StringUtils.isNotBlank(referValue)){
//            httpget.setHeader("Referer", referValue);
//        }
////        CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(httpget);
//        try {
////            HttpEntity entity = response.getEntity();
////            if(response.getStatusLine().getStatusCode() != 200 ){
//                //改变vpn连接标记
//            if(!isConnectVPN){
//                isConnectVPN = true;
//                LaunchVPNUtil launchVPNUtil = LaunchVPNUtil.getInstance();
//                launchVPNUtil.executeCmd(connectCommand);
//            }
//                Thread.currentThread().sleep(2000);
//                HttpResponse response = (CloseableHttpResponse) httpclient.execute(httpget);
//                HttpEntity entity = response.getEntity();
////            }
//            if (entity != null) {
//                input = entity.getContent();
//                output = new ByteArrayOutputStream();
//                byte[] buff = new byte[1024];
//                int len;
//                while ((len = input.read(buff,0,1024)) != -1){
//                    output.write(buff,0,len);
//                }
//                pictureDO.setPictureUrl(Base64.encodeBase64String(output.toByteArray()));
//            }
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } finally {
////            response.close();
//            if(input != null){
//                input.close();
//            }
//            if(output != null){
//                output.close();
//            }
//        }
//    }
}


